package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.service.other.domain.CityCompany;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.other.service.dto.CityCompanyDto;
import com.qiaofang.applet.service.other.service.dto.CompanyIntroductionDto;
import com.qiaofang.applet.service.other.service.mapstruct.CityCompanyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author QF2019
 */
@RestController
@Api(tags = "内容：公司介绍配置")
@RequestMapping("/api/companyIntroduction")
public class CompanyIntroductionController {

    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private CityCompanyMapper mapper;

    @Autowired
    private UserService userService;

    @ApiOperation("公司城市列表")
    @GetMapping
    public ResponseEntity<Page<CityCompanyDto>> list(Pageable pageable) {
        List<String> cityCodeList = userService.getCityCodeList(SecurityUtils.getCurrentUsername());
        Page<CityCompany> result = cityCompanyService.listAllPermissionCityCompany(SecurityUtils.getCorporationCode(),cityCodeList,pageable);
        return ResponseEntity.ok(result.map(mapper::toDto));
    }

    @ApiOperation("公司介绍设置-修改")
    @PutMapping
    public ResponseEntity<Object> updateCompanyIntroduction(@RequestBody CompanyIntroductionDto companyIntroductionDto){
        CityCompany cityCompany = new CityCompany();
        BeanUtils.copyProperties(companyIntroductionDto, cityCompany);
        cityCompanyService.saveCompany(cityCompany);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
